package com.kingtown.inventory.adapter;


import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import com.kingtown.inventory.R;
import com.kingtown.inventory.domain.dto.TaskDetailItemDto;

import java.util.List;

import static cn.hutool.core.io.resource.ResourceUtil.getResources;


public class TaskDetailListAdapter extends RecyclerView.Adapter<TaskDetailListAdapter.ViewHolder> implements View.OnClickListener {
    private List<TaskDetailItemDto> items;

    static class ViewHolder extends RecyclerView.ViewHolder {
        View taskView;
        TextView productName, productId, laborCosts, quantity, goldWeight, weight, price, status;

        public ViewHolder(@NonNull View view) {
            super(view);
            taskView = view;
            productName = (TextView) view.findViewById(R.id.txt_detail_product_name);
            productId = (TextView) view.findViewById(R.id.txt_detail_product_id);
            laborCosts = (TextView) view.findViewById(R.id.txt_detail_labor_costs);
            quantity = (TextView) view.findViewById(R.id.txt_detail_quantity);
            goldWeight = (TextView) view.findViewById(R.id.txt_detail_gold_weight);
            weight = (TextView) view.findViewById(R.id.txt_detail_weight);
            price = (TextView) view.findViewById(R.id.txt_detail_price);
            status = (TextView) view.findViewById(R.id.txt_detail_status);

        }
    }

    public TaskDetailListAdapter(List<TaskDetailItemDto> items) {
        this.items = items;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.task_detail_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        view.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        TaskDetailItemDto item = items.get(position);
        Log.i("onBindViewHolder", item.toString());
        holder.productName.setText(item.getProductName());
        holder.productId.setText(String.valueOf(item.getBarcode()));
        holder.laborCosts.setText(String.valueOf(item.getLaborCosts()));
        holder.quantity.setText(item.getQuantity().toPlainString());
        holder.goldWeight.setText(item.getGoldWeight().toPlainString());
        holder.weight.setText(item.getWeight().toPlainString());
        holder.price.setText(item.getPrice().toPlainString());
        holder.status.setText(item.getStatus());
        if (item.getStatus().equals("已盘")) {
            holder.taskView.setBackgroundColor(ContextCompat.getColor(holder.taskView.getContext(), R.color.primary));
        }else{
            holder.taskView.setBackgroundColor(ContextCompat.getColor(holder.taskView.getContext(), R.color.white));
        }
        holder.taskView.setTag(item);

    }

    @Override
    public int getItemCount() {
        return items.size();
    }

    private OnItemClickListener onItemClickListener = null;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public void onClick(View v) {
        if (onItemClickListener != null) {
            onItemClickListener.onClick(v, v.getTag());
        }
    }
}